import { randInt } from "./numFunc";

export namespace NStringFunc {
    export type TSnakeCaseSeparator = "_" | "-";
}

export function snakeCaseToCamelCase(str: string, splitCode: NStringFunc.TSnakeCaseSeparator = "-") {
    const reg = new RegExp(splitCode + "\\w", "g");
    return str.replace(reg, ($0: string, $1: string) => {
        return $1.toUpperCase();
    });
}

export function camelCaseToSnakeCase(str: string, splitCode: NStringFunc.TSnakeCaseSeparator = "-") {
    const reg = new RegExp("[A-Z]", "g");
    return str.replace(reg, ($0: string) => {
        return splitCode + $0.toLowerCase();
    });
}

export function ucFirst(str: string) {
    if (str.length <= 0) {
        return "";
    }
    const firstLetter = str.substr(0, 1).toUpperCase();
    const remainLetters = str.substr(1);
    return firstLetter + remainLetters;
}

export function lcFirst(str: string) {
    if (str.length <= 0) {
        return "";
    }
    const firstLetter = str.substr(0, 1).toLowerCase();
    const remainLetters = str.substr(1);
    return firstLetter + remainLetters;
}

export const letters = [
    "a", "b", "c", "d", "e", "f", "g",
    "h", "i", "j", "k", "l", "m", "n",
    "o", "p", "q", "r", "s", "t",
    "u", "v", "w", "x", "y", "z",
];

export const numList = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

export function randStr(len: number, seed: string[] = letters) {
    let str = "";
    while (len--) {
        str += seed[randInt(seed.length)];
    }
    return str;
}

export function sortString(strA: string, strB: string): 1 | -1 {
    if (!strA || !strA.trim()) {
        return 1;
    }
    if (!strB || !strB.trim()) {
        return -1;
    }
    strA = strA.trim();
    strB = strB.trim();
    return strA > strB ? 1 : -1;
}
